#include "stdafx.h"
#include "Interface.h"
#include "Main.h"

//-------------------------------------------------------------------------------------
Interface::~Interface(void)
{
}

void Interface::setupHikari()
{
	using namespace Hikari;
	hikariMgr = new HikariManager("");
	
	mainMenu = hikariMgr->createFlashOverlay("Main",main->mViewPort, main->mViewPort->getActualWidth(), main->mViewPort->getActualWidth(), Hikari::Center);
	mainMenu->load("..\\..\\SimpleMenu\\main.swf");
	mainMenu->setDraggable(false);
	mainMenu->setTransparent(true);
	mainMenu->bind("onStart", FlashDelegate(this, &Interface::onStart));
	mainMenu->hide();
	
	drawInterface = hikariMgr->createFlashOverlay("draw",main->mViewPort, main->mViewPort->getActualWidth(), main->mViewPort->getActualWidth(), Hikari::Center);
	drawInterface->load("..\\..\\SimpleMenu\\draw.swf");
	drawInterface->setDraggable(false);
	drawInterface->setTransparent(true);
	drawInterface->bind("onUseFootprint", FlashDelegate(this, &Interface::onUseFootprint));
	drawInterface->hide();

	pauseInterface = hikariMgr->createFlashOverlay("stop",main->mViewPort, main->mViewPort->getActualWidth(), main->mViewPort->getActualWidth(), Hikari::Center);
	pauseInterface->load("..\\..\\SimpleMenu\\stop.swf");
	pauseInterface->setDraggable(false);
	pauseInterface->setTransparent(true);
	pauseInterface->hide();

	chooseBackground = hikariMgr->createFlashOverlay("choose",main->mViewPort, main->mViewPort->getActualWidth(), main->mViewPort->getActualWidth(), Hikari::Center);
	chooseBackground->load("..\\..\\SimpleMenu\\choose.swf");
	chooseBackground->setDraggable(false);
	chooseBackground->setTransparent(true);
	chooseBackground->hide();
	chooseBackground->bind("onChoosed", FlashDelegate(this, &Interface::onChoosed));
	

	//	controls = hikariMgr->createFlashOverlay("Menu",main->mViewPort, main->mViewPort->getActualWidth(), main->mViewPort->getActualWidth()*0.4, Position(TopLeft));
	/*controls = hikariMgr->createFlashOverlay("Menu",main->mViewPort, main->mViewPort->getActualWidth(), main->mViewPort->getActualWidth()*0.4, Position(0.0f, -8.0f));
	controls->load("..\\..\\SimpleMenu\\SimpleMenu.swf");
	controls->setDraggable(false);
	controls->setTransparent(true);
	controls->bind("ButtonClicked", FlashDelegate(this, &Interface::onButtonClick));

	fileBrowser = hikariMgr->createFlashOverlay("FileBrowser",main->mViewPort, 300, 300, Position(Center));
	fileBrowser->load("..\\..\\SimpleMenu\\FileSelect.swf");
	fileBrowser->setDraggable(true);
	fileBrowser->setTransparent(true,true);
	fileBrowser->hide();
	fileBrowser->bind("Cancel", FlashDelegate(this, &Interface::onCancel));
	fileBrowser->bind("Ok", FlashDelegate(this, &Interface::onOk));
	fileBrowser->bind("ItemSelect", FlashDelegate(this, &Interface::onItemSelect));
	disk = new DiskObject();
	d_fileFilter=std::string("*.png");

	Prompt = hikariMgr->createFlashOverlay("Prompt",main->mViewPort, 300, 200, Position(Center));
	Prompt->load("..\\..\\SimpleMenu\\Prompt.swf");
	Prompt->setDraggable(true);
	Prompt->setTransparent(true,true);
	Prompt->hide();
	Prompt->bind("Param", FlashDelegate(this, &Interface::onParam));
	Prompt->bind("Cancel", FlashDelegate(this, &Interface::onClose));*/
}
Hikari::FlashValue Interface::onStart(Hikari::FlashControl* caller, const Hikari::Arguments& args)
{
	main->gm->gameState = GameManager::INIT_GAME;
	mainMenu->hide();
	return FLASH_VOID;
}
Hikari::FlashValue Interface::onChoosed(Hikari::FlashControl* caller, const Hikari::Arguments& args)
{
	main->gm->gameState = GameManager::DRAWING;
	main->gm->drawingBackground = args[0].getNumber();
	
	main->useBgImg = args[0].getNumber();
	chooseBackground->hide();
	drawInterface->show();
	return FLASH_VOID;
}

std::vector<std::string>  Interface::parse(std::string l, char delim) 
{
	std::replace(l.begin(), l.end(), delim, ' ');
	std::istringstream stm(l);
	std::vector<std::string> tokens;
	for (;;) {
		std::string word;
		if (!(stm >> word)) break;
		tokens.push_back(word);
	}
	return tokens;
}

Hikari::FlashValue Interface::onClose(Hikari::FlashControl* caller, const Hikari::Arguments& args)
{
	Prompt->hide();
	return FLASH_VOID;
}

Hikari::FlashValue Interface::onParam(Hikari::FlashControl* caller, const Hikari::Arguments& args)
{
	Prompt->hide();
	float fParam = args[0].getNumber();
	int iParam = args[0].getNumber();

	if (currntOperation == SAVE)
	{
		Ogre::String str = "..\\..\\media\\materials\\textures\\";
		str.append(args[0].getString());
		str.append(".png");
//		main->saveImage(str.c_str());
	}
/*	else if (currntOperation == FILTER_GAUSSIAN_N)
	{
		printf("\nN: %d\n", iParam);
		main->Filter_Gaussian_N(iParam);
	}
	else if (currntOperation == ROTATE)
	{
		printf("\nThe degree of rotate: %.2lf\n", fParam);
		main->Rotate(fParam);
	}
	else if (currntOperation == RESIZE)
	{
		printf("\nSize: %.2lf\n", fParam);
		main->Resize(fParam);
	}*/

	return FLASH_VOID;
}

Hikari::FlashValue Interface::onButtonClick(Hikari::FlashControl* caller, const Hikari::Arguments& args)
{
	using namespace Hikari;
	Ogre::String funcArg = args[0].getString();

	
	if (funcArg=="Open")
	{
		fileBrowser->show();
		fileBrowser->focus();
		disk->setCurrentVFolder(disk->getApplicationFolder());
		disk->setCurrentVFolder("..\\..\\media\\materials\\textures");
		updateFolderList();
		currntOperation=OPEN;
	}
	else if(funcArg=="Save")
	{
		Prompt->show();
		fileBrowser->focus();
		Ogre::String s = "Input the name of the file";
		Prompt->callFunction("reset",Args(s));
		currntOperation= SAVE;
	}
	else if(funcArg=="Exit")
	{
//		main->mShutDown=true;
	}
	else if(funcArg=="Gray")
	{
//		main->Gray();
	}
	else if(funcArg == "Hide")
	{
		flip();	
	}

	return FLASH_VOID;
}

Hikari::FlashValue Interface::onCancel(Hikari::FlashControl* caller, const Hikari::Arguments& args)
{
	using namespace Hikari;
	fileBrowser->hide();
	return FLASH_VOID;
}

Hikari::FlashValue Interface::onOk(Hikari::FlashControl* caller, const Hikari::Arguments& args)
{
	using namespace Hikari;
	fileBrowser->hide();

	if(currntOperation == OPEN)
	{
		//printf("PATH: %s",filePath);
//		main->openImage(filePath.c_str());
	}
	else if(currntOperation == SAVE)
	{
		filePath = disk->getCurrentVFolder();
		std::string res= fileBrowser->callFunction("readText").getString();
		filePath+=res;
		printf("PATH: %s",filePath);

	}
	
	/*else if (currntOperation == COMP_OVER)
	{
		main->loadSecondaryImge(filePath.c_str());
		main->Comp_Over();
	}
	else if (currntOperation == COMP_IN)
	{
		main->loadSecondaryImge(filePath.c_str());
		main->Comp_In();
	}
	else if (currntOperation == COMP_OUT)
	{
		main->loadSecondaryImge(filePath.c_str());
		main->Comp_Out();
	}
	else if (currntOperation == COMP_ATOP)
	{
		main->loadSecondaryImge(filePath.c_str());
		main->Comp_Atop();
	}
	else if (currntOperation == COMP_XOR)
	{
		main->loadSecondaryImge(filePath.c_str());
		main->Comp_Xor();
	}*/

	return FLASH_VOID;
}
Hikari::FlashValue Interface::onItemSelect(Hikari::FlashControl* caller, const Hikari::Arguments& args)
{
	using namespace Hikari;

	std::string text = args.at(0).getString(); 
	fileName=text;
	if (text == "..")
	{
		//disk->upLevel();
		//updateFolderList();
		//d_result = disk->getCurrentVFolder();
		//fileBrowser->callFunction("setName",Args(d_result));
	}
	else if(text.find(".",1)!=std::string::npos)
	{
		//filePath=disk->getCurrentVFolder()+ "\\" +text;
		filePath = text;
	}
	else
	{
		std::string path = disk->getCurrentVFolder();
		if (path.size() > 3) path.append("\\");
		path.append(text);
		disk->setCurrentVFolder(std::string(path.c_str()));
		updateFolderList();
		d_result = disk->getCurrentVFolder();
		fileBrowser->callFunction("setName",Args(text));
	}

	return FLASH_VOID;
}
void Interface::updateFolderList()
{
	using namespace Hikari;
	using namespace std;
	string path ="";
	d_itemList.clear();
	d_itemList = disk->getFolders();
	for(int i=0;i<disk->getFiles(d_fileFilter).size();i++)
	{
		d_itemList.push_back(disk->getFiles(d_fileFilter)[i]);
		
	}
	for (int n = 0; n < d_itemList.size(); n++)
	{
		path.append(d_itemList[n].c_str());
		path.append("|");
	}
	fileBrowser->callFunction("refreshList",Args(path));
}

void Interface::flip()
{
	if (controls->getVisibility())
		controls->hide();
	else
		controls->show();
}

Hikari::FlashValue Interface::onUseFootprint( Hikari::FlashControl* caller, const Hikari::Arguments& args )
{
	Ogre::String strokeType = args.at(0).getString();
	if(strokeType == "1")
		main->useStroke = Main::StrokeType::FP01;
	else if(strokeType == "2")
		main->useStroke = Main::StrokeType::FP02;
	else if(strokeType == "3")
		main->useStroke = Main::StrokeType::FP03;
	else
		printf("The stroke value does not exist! \n");

	return FLASH_VOID;
}